package vn.com.epi.data.statistic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Category {

	private static Map<Integer, Integer> map;
	
	public static void statistic() throws IOException {
		map = new HashMap<Integer, Integer>();
		String[] filenames = new String[] {"input/2014-8-20", "input/2014-8-21"};
		for ( int i = 0; i < filenames.length; i++) {
			File[] files = new File(filenames[i]).listFiles();
			for (File file : files) {
				if (file.isFile()) {
					BufferedReader reader = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = reader.readLine()) != null) {
						String[] chunks = line.split(",");
						int categoryId = Integer.parseInt(chunks[4]);
						String user = chunks[1];
						if (!user.startsWith("RANDOM") && user.length() != 36) {
							if (map.containsKey(categoryId)) {
								int num = map.get(categoryId);
								num++;
								map.put(categoryId, num);
							} else
								map.put(categoryId, 1);
						}
					}
					reader.close();
					System.out.println(file.getAbsolutePath());
				}
			}
		}
		List<Integer> list = new ArrayList<Integer>();
		int index = 0;
		for (Map.Entry<Integer, Integer> entry:map.entrySet()) {
			index++;
			System.out.println(index +": " + entry.getKey() + "\t" + entry.getValue());
			list.add(entry.getKey());
		}
		Collections.sort(list);
		String s = "";
		for (int i: list) {
			s += i + ",";
		}
		System.out.println(s);
	}
	
	public static void main(String[] args) throws IOException {
		statistic();
	}
			
}
